﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using PersianLocalization;
using Telerik.WinControls.UI.Localization;

namespace TelerikForms.PL
{
    public partial class BankForm : Telerik.WinControls.UI.RadForm
    {
        public BankForm()
        {
            InitializeComponent();
            RadGridLocalizationProvider.CurrentProvider = new PersianRadGridLocalizationProvider();
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.GetCultureInfo("fa-IR"));                
        }

        //private void addRowButton_Click(object sender, EventArgs e)
        //{
        //    bankDGV.AllowAddNewRow = ! bankDGV.AllowAddNewRow;
        //}

        private void cancelButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void updateButton_Click(object sender, EventArgs e)
        {
            try
            {
                if (bankDGV.CurrentRow != null)
                {
                    ChequeManagement.BLL.BLLBank bankObj = new ChequeManagement.BLL.BLLBank();
                    bankObj.BankID = (int)bankDGV.CurrentRow.Cells["bankIDCol"].Value;
                    bankObj.BankName = bankNameTextBox.Text;

                    ChequeManagement.BLL.BLLBankFactory bankFactory = new ChequeManagement.BLL.BLLBankFactory();
                    bankFactory.Edit(bankObj, bankObj.BankID);
                }
                else if (bankNameTextBox.Text != "")
                {
                    ChequeManagement.BLL.BLLBank bankObj = new ChequeManagement.BLL.BLLBank();
                    bankObj.BankName = bankNameTextBox.Text;

                    ChequeManagement.BLL.BLLBankFactory bankFactory = new ChequeManagement.BLL.BLLBankFactory();
                    bankFactory.Add(bankObj);
                }
                else
                {
                    MessageBox.Show("لطفا نام بانک را وارد نمایید");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void BankForm_Load(object sender, EventArgs e)
        {
            try
            {
                DataTable bankDT = new DataTable();
                ChequeManagement.BLL.BLLBankFactory bllBankObj = new ChequeManagement.BLL.BLLBankFactory();
                bllBankObj.GetAll(bankDT);

                bankDGV.DataSource = bankDT;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bankDGV_CurrentRowChanged(object sender, CurrentRowChangedEventArgs e)
        {
            try
            {
                GridViewRowInfo currentRow = e.CurrentRow;
                if (currentRow != null && !(currentRow is GridViewNewRowInfo))                
                    bankNameTextBox.Text = currentRow.Cells["bankNameCol"].Value.ToString();                
                else                
                    bankNameTextBox.Text = "";
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }

        private void bankDGV_CellValueChanged(object sender, GridViewCellEventArgs e)
        {
            GridViewRowInfo currentRow = bankDGV.CurrentRow;
            if (currentRow is GridViewNewRowInfo && ! string.IsNullOrEmpty( currentRow.Cells["choiceCol"].Value.ToString()))
            {
                currentRow.Cells["choiceCol"].Value = true;
            }
            if (e.Column.Name.ToLower() == "banknamecol")
            {
                bankNameTextBox.Text = e.Value.ToString();
            }            
        }

        private void RadTextBox_TextChanged(object sender, EventArgs e)
        {
            if (bankDGV.CurrentRow.Cells["bankNameCol"].Value !=null)
                bankDGV.CurrentRow.Cells["bankNameCol"].Value = bankNameTextBox.Text;
        }

        //private void deleteRowButton_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        GridViewDataRowInfo dataRowInfo = bankDGV.CurrentRow as GridViewDataRowInfo;
        //        if (dataRowInfo != null)
        //        {
        //            int bankID = (int)dataRowInfo.Cells["bankIDCol"].Value;
        //            ChequeManagement.BLL.BLLBankFactory bllBankObj = new ChequeManagement.BLL.BLLBankFactory();
        //            bllBankObj.Remove(bankID);

        //            bankDGV.Rows.Remove(dataRowInfo);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        MessageBox.Show(ex.Message);
        //    }
        //}
    }
}
